package cn.iocoder.yudao.module.train.service.courseclasses;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.CourseClassesPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.CourseClassesSaveReqVO;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.VipClassesPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.courseclasses.vo.VipClassesSaveReqVO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.VipClassesCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.CourseClassesDO;
import cn.iocoder.yudao.module.train.dal.dataobject.courseclasses.VipClassesDO;
import cn.iocoder.yudao.module.train.dal.mysql.courseclasses.VipClassesMapper;
import cn.iocoder.yudao.module.train.dal.mysql.coursecommodity.CourseCommodityMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.COURSE_CLASSES_NOT_EXISTS;

/**
 * 班级 Service 实现类
 *
 * @author waynelam
 */
@Service
@Validated
public class VipClassesServiceImpl implements VipClassesService {

    @Resource
    private VipClassesMapper vipClassesMapper;

    @Resource
    private CourseCommodityMapper courseCommodityMapper;

    @Override
    public Long createVipClasses(VipClassesSaveReqVO createReqVO) {
        // 插入
        VipClassesDO courseClasses = BeanUtils.toBean(createReqVO, VipClassesDO.class);
        vipClassesMapper.insert(courseClasses);

        // 返回
        return courseClasses.getId();
    }

    @Override
    public void updateVipClasses(VipClassesSaveReqVO updateReqVO) {
        // 校验存在
        validateVipClassesExists(updateReqVO.getId());
        // 更新
        VipClassesDO updateObj = BeanUtils.toBean(updateReqVO, VipClassesDO.class);
        vipClassesMapper.updateById(updateObj);
    }

    @Override
    public void deleteVipClasses(Long id) {
        // 校验存在
        validateVipClassesExists(id);
        // 删除
        vipClassesMapper.deleteById(id);
    }

    @Override
        public void deleteVipClassesListByIds(List<Long> ids) {
        // 删除
        vipClassesMapper.deleteByIds(ids);
        }


    private void validateVipClassesExists(Long id) {
        if (vipClassesMapper.selectById(id) == null) {
            throw exception(COURSE_CLASSES_NOT_EXISTS);
        }
    }

    @Override
    public VipClassesCommodityDTO getVipClasses(Long id) {
        return vipClassesMapper.selectById(id);
    }

    @Override
    public PageResult<VipClassesCommodityDTO> getVipClassesPage(VipClassesPageReqVO pageReqVO) {
        return vipClassesMapper.selectPage(pageReqVO);
    }

    @Override
    public VipClassesDO getVipClassesByCommodityId(Long commodityId) {
        return vipClassesMapper.selectByCommodityId(commodityId);
    }


    @Override
    public List<VipClassesCommodityDTO> getVipClassesList(VipClassesPageReqVO pageReqVO) {
        return vipClassesMapper.selectList(pageReqVO);
    }
}
